package com.dating.base.module;

import java.util.List;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.Lang;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.dating.base.bean.Permission;
import com.dating.base.bean.RolePermission;
import com.dating.base.bean.User.Type;
import com.dating.base.bean.UserPermission;
import com.dating.base.service.RolePermissionService;
import com.dating.base.service.UserPermissionService;
import com.dating.core.entries.Result;
import com.dating.core.ext.BaseModule;

public class UserRolePermissionModule extends BaseModule {

	@Inject
	private UserPermissionService userPermissionService ;
	
	@Inject
	private RolePermissionService rolePermissionService ;
	
	@Override
	public String _getNameSpace() {
		return "userRolePermission";
	}
	
	/**
	 * to 设置权限
	 * @return
	 */
	@At("userRole/toPermission")
	@Ok("beetl:templates/pages/base/user_role_permission.html")
	public Result toList(){
		return Result.success();
	}
	
	@At("userRole/getPermission")
	@Ok("json")
	public Result getPermission(@Param("..")UserPermission userPer, @Param("..")RolePermission rolePer, String op){
		String[] ops = {"up" , "rp"};
		if(!Lang.contains(ops, op)){
			return Result.fail("操作参数错误！");
		}
		List<Permission> perms = null;
		if(op.equals("up")){
			userPer.setType(Type.PLATFORM);
			perms = userPermissionService.getPermissions(userPer);
		}
		if(op.equals("rp")){
			perms = rolePermissionService.getPermissions(rolePer.getRoleId());
		}
		return Result.success().addData("list", perms);
	}
	
	/**
	 * 设置权限
	 * @param permission
	 * @return
	 */
	@At("userRole/setPermission")
	@Ok("json")
	public Result setPermission(@Param("..")UserPermission userPer, @Param("..")RolePermission rolePer, String op, String status){
		String[] ops = {"up" , "rp"};
		String [] statuses = {"Y", "N"};
		if(!Lang.contains(ops, op) || !Lang.contains(statuses, status)){
			return Result.fail("操作参数错误！");
		}
		
		if(op.equals("up")){
			userPer.setType(Type.PLATFORM);
			userPermissionService.setPermission(userPer, status);
		}
		if(op.equals("rp")){
			rolePermissionService.setPermission(rolePer, status);
		}
		return Result.success();
	}
}
